package com.sihan.swvp.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.filter.OncePerRequestFilter;

/**
 * session信息拦截器
 * 
 * @author jch
 * 
 */
public class SessionFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterchain)
			throws ServletException, IOException {
		String uri = request.getRequestURI();
		String context = request.getContextPath();
		boolean filter = context.equals("/" + uri.replace("/", ""))
				|| uri.endsWith(".jsp") || uri.contains(".do");

		// TODO Auto-generated method stub

		HttpSession session = request.getSession();
		if (filter && null == session.getAttribute("u")
				&& !uri.endsWith("login.jsp") && !uri.endsWith("login.do")
				&& !uri.endsWith("getVimg.do") && !uri.contains("test/") && !uri.contains("tsk/") && !uri.contains("getVideoPic.do")) {
			
			if ((request.getHeader("X-Requested-With") != null && "XMLHttpRequest"
					.equals(request.getHeader("X-Requested-With")))) {
				response.sendError(409, "您长时间未操作，请刷新页面");
			} else {
				response.sendRedirect(request.getContextPath() + "/login.jsp");
			}
		} else {
			filterchain.doFilter(request, response);
		}

	}

}
